package com.syh.jn.scm.domain.operation.entity.project;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.spring.SpringContextUtils;
import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.core.ddd.domain.AbstractSimpleAggregate;
import com.syh.jn.scm.domain.operation.repository.project.OperationProjectReadRepository;
import com.syh.jn.scm.dto.operation.project.OperationProjectMto;
import com.syh.jn.scm.dto.operation.project.OperationProjectSto;
import com.syh.jn.scm.exception.BusinessException;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Date:2018/7/13
 * Author:Clown
 * 运营平台直营项目;
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class OperationProject extends AbstractSimpleAggregate<Long> {

  /**
   * 创建直营项目
   * @param sto
   * @return
   */
  public void create(OperationProjectSto sto){
    //初始化直营项目
    this.status = 1;
    this.businessEntityId = new Long(0);
    initDefault();
    ExtBeanUtils.copyProperties(sto,this);
  }

  /**
   * 修改直营项目
   * @param mto
   */
  public void modify(OperationProjectMto mto){
    ExtBeanUtils.copyProperties(mto, this);
    this.lastUpdateTimeStamp = System.currentTimeMillis();
  }

  /**
   * 删除直营项目
   */
  public void destroy() {
    OperationProjectReadRepository projectReadRepository = (OperationProjectReadRepository) SpringContextUtils.getBean(OperationProjectReadRepository.class);
    Wrapper we = Wrapper.build();
    we.append(Restrictions.rightLike("catalogCode",this.catalogCode+"-"));
    List<OperationProject> projectList = projectReadRepository.getList(we);
    if (projectList.size() > 0 && projectList != null) {
      throw new BusinessException("请先删除子项目");
    }
  }

  /**
   * 启用项目
   */
  public void enabled() {
    this.status = 1;
    List<String> catalogIds = new ArrayList<>(Arrays.asList(this.catalogCode.split("-")));
    if(catalogIds.size() > 1)
      catalogIds.remove(catalogIds.size()-1);

    Wrapper we = Wrapper.build();
    we.append(Restrictions.in("id",catalogIds.toArray()));
    OperationProjectReadRepository projectRepository = (OperationProjectReadRepository) SpringContextUtils.getBean(OperationProjectReadRepository.class);
    this.parents = projectRepository.getList(we);
    this.parents.forEach(parent ->{
      parent.setStatus(this.status);
    });
  }

  /**
   * 停用项目
   */
  public void disabled() {
    this.status = -1;

    Wrapper we = Wrapper.build();
    we.append(Restrictions.rightLike("catalogCode",this.catalogCode+"-"));
    OperationProjectReadRepository projectRepository = (OperationProjectReadRepository) SpringContextUtils.getBean(OperationProjectReadRepository.class);
    this.parents = projectRepository.getList(we);
    this.parents.forEach(parent ->{
      parent.setStatus(this.status);
    });
  }

    /**
   * 归属公司
   */
  private Long businessEntityId;
  /**
   * 状态（-1：停用 1：启用）
   */
  private Short status;
  /**
   * 上级项目id
   */
  private Long supId;
  /**
   * 父项目
   */
  private List<OperationProject> parents;
  /**
   * 子项目
   */
  private List<OperationProject> subProjects;
  /**
   * 项目编码
   */
  private String code;
  /**
   * 项目名称
   */
  private String name;
  /**
   * 排序等级
   */
  private Integer orderLevel;
  /**
   * 目录树    格式：1-11-12  1：父父类id，11：父类id，12：当前id
   */
  private String catalogCode;
  /**
   * 描述
   */
  private String description;


}
